<template>
<div class="system-role">
    <query-box :queryForm="queryForm" @search="handleSearch(true)" @reset="handleReset"></query-box>
    <table-list :tableData="tableData" @flash="handleSearch" @add="handleAdd" @check="handleCheck" @export="handleExport"></table-list>
    <add-dialog v-if="addForm.show" :addForm="addForm" @save="handleSave"></add-dialog>
    <check-dialog v-if="checkForm.show" :checkForm="checkForm"></check-dialog>
</div>
</template>

<script>
import AddDialog from './AddDialog'
import TableList from './TableList'
import QueryBox from './QueryBox'
import CheckDialog from './CheckDialog'
import moment from 'moment'
export default {
    name: 'Role',
    components: {
        AddDialog,
        QueryBox,
        TableList,
        CheckDialog
    },
    data () {
        return {
            tableData: {
                isSearch: false,
                total: 0,
                pageSize: 10,
                currentPage: 1,
                loading: false,
                rows: []
            },
            addForm: {
                show: false,
                title: '新增',
                treeData: [],
                form: {}
            },
            checkForm: {
                show: false,
                title: '新增',
                treeData: [],
                form: {}
            },
            queryForm: {
                roleName: null,
                time: ['', '']
            }
        }
    },
    created () {
        this.init()
    },
    methods: {
        handleExport () {
            const {
                roleName,
                time
            } = this.queryForm
            const params = {
                roleName,
                createTimeFrom: time[0] && moment(time[0]).format('YYYY-MM-DD HH:mm:ss'),
                createTimeTo: time[1] && moment(time[1]).format('YYYY-MM-DD HH:mm:ss')
            }
            this.$download('/role/excel', params).then(res => {
                if (res) {
                    this.$message.success('导出成功')
                }
            })
        },
        handleSave () {
            let flag = this.addForm.title === '新增角色'
            this[flag ? '$formDataType2' : '$put']('/role', this.addForm.form).then(res => {
                if (res) {
                    this.$message.success(flag ? '新增成功' : '修改成功')
                    this.addForm.show = false
                    this.init()
                }
            })
        },
        handleSearch (flag) {
            if (flag) {
                this.tableData.currentPage = 1
            }
            this.tableData.isSearch = true
            this.init()
        },
        init () {
            this.tableData.loading = true
            let params = {
                pageNum: this.tableData.currentPage,
                pageSize: this.tableData.pageSize,
                roleName: this.queryForm.roleName,
                createTimeFrom: this.queryForm.time[0],
                createTimeTo: this.queryForm.time[1]
            }
            this.$get('/role', params).then((res) => {
                if (res) {
                    this.tableData.rows = res.rows
                    this.tableData.total = res.total
                }
            }).finally(() => {
                this.tableData.loading = false
            })
        },
        handleAdd (item) {
            if (!item) {
                this.addForm.title = '新增角色'
                this.addForm.form = {
                    roleName: null,
                    remark: null,
                    menuId: '0'
                }
            } else {
                this.addForm.title = '修改角色'
                this.addForm.form = {
                    roleName: item.roleName,
                    remark: item.remark,
                    roleId: item.roleId
                }
            }
            this.getTreeData()
            this.addForm.show = true
        },
        getTreeData () {
            this.$get('/menu').then(res => {
                if (res) {
                    this.addForm.treeData = res.rows.children
                    this.checkForm.treeData = res.rows.children
                }
            })
        },
        handleReset () {
            this.queryForm = {
                roleName: null,
                time: ['', '']
            }
            this.tableData.currentPage = 1
            this.init()
        },
        handleCheck (item) {
            this.checkForm.title = '角色信息'
            this.checkForm.form = {
                roleName: item.roleName,
                remark: item.remark,
                createTime: item.createTime,
                modifyTime: item.modifyTime,
                roleId: item.roleId
            }
            this.getTreeData()
            this.checkForm.show = true
        }
    }
}
</script>
